package com.inkrl.user.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.inkrl.user.domain.User;
import com.inkrl.user.enums.ResCode;
import com.inkrl.user.enums.UserParam;
import com.inkrl.user.service.UserService;
import com.jt.common.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService = null;
    /**
     * @author InkReturn
     * @RequestType  Get
     * @url /user/token
     * @Description : 用于用登录
     * @date 2020/9/11 14:27
     * @methodName loginUser
     * @param username  用户名
     * @param password  密码
     * @param loginPage 要登录的是哪个页面
     * @return JSON
     * {
     *  uname:(x) ,
     *  resCode:(x)
     * }
     */
    @ResponseBody
    @CrossOrigin
    @RequestMapping(value = "/token",method = RequestMethod.GET)
    public SysResult loginUser(String username,String password,int loginPage) throws JsonProcessingException {
        HashMap<String, String> responseMap = new HashMap<String, String>();
//        try {

            responseMap=userService.userLogin(username,password);
            return SysResult.build(200,"请求完成",responseMap);

//        } catch (Exception e) {
////
//            responseMap.put("msg","访问出错");
//            responseMap.put("resCode", ResCode.ERRO);
//            return SysResult.build(200,"出错",responseMap);
//        }
    }

    /**
     * @author InkReturn
     * @Description  用于注册用户
     * @date 2020/9/13 9:32
     * @methodName addUser
     * @RequestType  Post
     * @url /user
     * @param user 封装用户信息
     * @return Json
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    public SysResult addUser(@Validated User user){
        HashMap<String, String> responseMap = new HashMap<String, String>();
        //校验前端发送过来的数据
        //调用userService的isDuplicate方法校验用户名 返回值true代表他是重复的

        if(userService.isDuplicate(user.getPhone(), UserParam.PHONE)){
            //如果不是重复就返回xxx不可以
            responseMap.put("msg","手机号不可用");
            responseMap.put("resCode", ResCode.ERRO);
            return SysResult.build(200,"请求完成",responseMap);
        }

        if(userService.isDuplicate(user.getPhone(), UserParam.PHONE)){
            //如果不是重复就返回xxx不可以
            responseMap.put("msg","用户名不可用");
            responseMap.put("resCode", ResCode.ERRO);
            return SysResult.build(200,"请求完成",responseMap);
        }
        //如果数据有误则返回错误
        //校验手机验证码
        //TODO
        userService.addUser(user);
        responseMap.put("msg","用户已注册");
        responseMap.put("resCode", ResCode.OK);
        return SysResult.build(200,"请求完成",responseMap);
    }
    /**
     * @author InkReturn
     * @Description : 修改用户当前信息
     * @date 2020/9/13 9:38
     * @methodName editUser
     * @RequestType  PUT
     * @url /user
     * @param user 封装用户信息
     * @return Json字符串
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.PUT)
    public SysResult editUser(User user){
        return null;
    }

    /**
     * @author InkReturn
     * @Description  校验需求的属性是否重复
     * @date 2020/9/13 9:55
     * @methodName checkDuplicate
     * @RequestType  GET
     * @url /user/{paramName}
     * @param request 请求信息
     * @param paramName 请求名字
     * @return Json
        {
            msg:(x)
            resCode:(x)
        }
     */
    @ResponseBody
    @RequestMapping(value = "/{paramName}",method = RequestMethod.GET)
    public SysResult checkDuplicate(@PathVariable String paramName, HttpServletRequest request){
        HashMap<String, String> responseMap = new HashMap<String, String>();
        String param = request.getParameter(paramName);
        String str;
        switch (paramName){
            case UserParam.PHONE:
                str = "手机号";
                break;

            case UserParam.USERNAME:
                str = "用户名";
                break;

            default:
                str = paramName;
        }
        //调用userService的isDuplicate方法校验用户名 返回值true代表他是重复的
        if(userService.isDuplicate(param,paramName)){
            //如果不是重复就返回xxx不可以
            responseMap.put("msg",str+"不可用");
            responseMap.put("resCode", ResCode.ERRO);
            return SysResult.build(200,"请求完成",responseMap);
        }
        //如果不是重复就返回不重复的信息
        responseMap.put("msg",str+"可用");
        responseMap.put("resCode", ResCode.OK);
        return SysResult.build(200,"请求完成",responseMap);
    }

}
